from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options

# 设置 Selenium 选项
chrome_options = Options()
chrome_options.add_argument("--headless")  # 无头模式，不弹出浏览器
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--window-size=1920x1080")

# ChromeDriver 需要指定路径
service = Service("/Users/chenele/Downloads/chromedriver-mac-arm64/chromedriver")  # 需要下载 ChromeDriver 并替换路径
driver = webdriver.Chrome(service=service, options=chrome_options)

# 打开网页
url = "https://www.mi.com/shop/comment/20580.html"
driver.get(url)

# 等待 JavaScript 加载完数据
driver.implicitly_wait(5)

# 提取评论内容（需调整具体 XPath）
comments = driver.find_elements(By.CLASS_NAME, "comment-content")

# 输出评论
for idx, comment in enumerate(comments, 1):
    print(f"评论 {idx}: {comment.text.strip()}")

# 关闭浏览器
driver.quit()